<?php
class Ssh2 {
        var $conn;
        var $error;
        var $stream;

        function login($host,$user,$pass,$port=22) {

                 if ($this->connect($host,$port)) {

                     if ($this->auth($user,$pass)) { return 1; }
                     else { return 0; }
                 } else { return 0; }
        }

        function connect($host,$port=22) {
              if ( $this->conn=ssh2_connect($host, $port) ) 
		{ return 1;} 
	     else 
		{ $this->error="[x] Can not connected to $host $port"; return 0; }
        }

        function auth($u,$p) {
               if (ssh2_auth_password($this->conn, $u, $p)) {
                       return 1;
               } else { $this->error="Invalid login"; return 0; }
       }
        function cmd($cmd) {
                $this->stream=ssh2_exec($this->conn, $cmd);
                stream_set_blocking( $this->stream, true );
		return stream_get_contents($this->stream);
        }

        function send($localFile,$remoteFile,$permision) {
		 if (!ssh2_scp_send($this->conn, $localFile, $remoteFile, $permision)) {
			$this->error="Can not transfer file : $localFile to $remoteFile"; 
			return 0; 
		}
		return 1;
        }

        function get($remoteFile,$localFile) {
                if (!ssh2_scp_recv($this->conn, $remoteFile, $localFile)) {
			$this->error="Can not get file : $remoteFile to $localFile"; 
			return 0; 
		}
		return 1;
        }

}
?> 
